package cn.yuan.xiaoyu.wangmodule.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import cn.yuan.xiaoyu.R;

/**
 * Created by yukuoyuan on 2017/3/2.
 * 这是一个旺旺的点击添加的时候弹出的弹窗
 */

public class WangWangAddPoPuwindow extends PopupWindow implements View.OnClickListener {
    private final Context context;
    private OnBtnClickListener onBtnClickListener;
    private RelativeLayout rl_main_popu_add_group;
    private RelativeLayout rl_main_popu_add_friends;
    private RelativeLayout rl_main_popu_add_scan;
    private RelativeLayout rl_main_popu_add_fadeback;

    public interface OnBtnClickListener {
        void onBtnGroupClickListener();

        void onBtnFriendsClickListener();

        void onBtnScanClickListener();

        void onBtnFadebackClickListener();
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public WangWangAddPoPuwindow(Context context) {
        this.context = context;
        View view = View.inflate(context, R.layout.popu_wangwang_add, null);
        setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setContentView(view);
        setFocusable(true);//设置获取焦点
        setTouchable(true);//设置可以触摸
        setOutsideTouchable(true);//设置外边可以点击
        setBackgroundDrawable(new BitmapDrawable());
        //设置点击事件的监听
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    //不在该界面的点击事件
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        initView(view);
        initListener();
    }

    private void initView(View view) {
        rl_main_popu_add_group = (RelativeLayout) view.findViewById(R.id.rl_main_popu_add_group);
        rl_main_popu_add_friends = (RelativeLayout) view.findViewById(R.id.rl_main_popu_add_friends);
        rl_main_popu_add_scan = (RelativeLayout) view.findViewById(R.id.rl_main_popu_add_scan);
        rl_main_popu_add_fadeback = (RelativeLayout) view.findViewById(R.id.rl_main_popu_add_fadeback);
    }

    private void initListener() {
        rl_main_popu_add_group.setOnClickListener(this);
        rl_main_popu_add_friends.setOnClickListener(this);
        rl_main_popu_add_scan.setOnClickListener(this);
        rl_main_popu_add_fadeback.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_main_popu_add_group:
                if (onBtnClickListener != null) {
                    onBtnClickListener.onBtnGroupClickListener();
                }
                dismiss();
                break;
            case R.id.rl_main_popu_add_friends:
                if (onBtnClickListener != null) {
                    onBtnClickListener.onBtnFriendsClickListener();
                }
                dismiss();
                break;
            case R.id.rl_main_popu_add_scan:
                if (onBtnClickListener != null) {
                    onBtnClickListener.onBtnScanClickListener();
                }
                dismiss();
                break;
            case R.id.rl_main_popu_add_fadeback:
                if (onBtnClickListener != null) {
                    onBtnClickListener.onBtnFadebackClickListener();
                }
                dismiss();
                break;

        }
    }
}
